package util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBUtils {
	private static DataSource dataSource;
	
	static {
		String driver = "com.mysql.cj.jdbc.Driver";
		String url = "jdbc:mysql://localhost:3306/scott_db?charset=utf8&serverTimezone=GMT%2B8";
		String username = "root";
		String password = "123456";
		
		// 创建druid连接池
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName(driver);
		ds.setUrl(url);
		ds.setUsername(username);
		ds.setPassword(password);
		ds.setMinIdle(10);
		ds.setInitialSize(10);
		ds.setMaxActive(30);
		// 赋值给全局私有属性
		dataSource = ds;
	}
	
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
}


